/*
 * ConfDialog.java
 *
 * Created on __DATE__, __TIME__
 */
package com.wyps.ui;

import com.wyps.entity.Path;
import com.wyps.entity.PathType;
import com.wyps.entity.tools.CommonUtils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import org.openqa.selenium.By;

/**
 *
 * @author __USER__
 */
public class ConfDialog extends javax.swing.JDialog implements ActionListener {

    private Path path;

    public Path getPath() {
        return path;
    }

    public void setPath(Path path) {
        this.path = path;
    }

    private JTextField target;

    public JTextField getTarget() {
        return target;
    }

    public void setTarget(JTextField target) {
        this.target = target;
    }
    private String[] pathType = new String[]{
        "id", "className", "cssSelector", "linkText", "name", "partialLinkText", "tagName", "xpath"};

    /**
     * Creates new form ConfDialog
     */
    public ConfDialog(JTextField target, Path path, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.target = target;
        this.path = path;
       
        initComponents();
         selectPath(path);
        save.addActionListener(this);
    }

    public void selectPath(Path path) {
        if (!CommonUtils.isEmpty(path.getValue())) {
            switch (path.getType()) {
                case className:
                    pathtype.setSelectedItem(pathType[1]);
                    break;
                case cssSelector:
                    pathtype.setSelectedItem(pathType[2]);
                    break;
                case id:
                    pathtype.setSelectedItem(pathType[0]);
                    break;
                case linkText:
                    pathtype.setSelectedItem(pathType[3]);
                    break;
                case name:
                    pathtype.setSelectedItem(pathType[4]);
                    break;
                case partialLinkText:
                    pathtype.setSelectedItem(pathType[5]);
                    break;
                case tagName:
                    pathtype.setSelectedItem(pathType[6]);
                    break;
                case xpath:
                    pathtype.setSelectedItem(pathType[7]);
                    break;
                default:
                    break;
            }
            pathVal.setText(path.getValue());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        pathtype = new javax.swing.JComboBox();
        pathVal = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        save = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        pathtype.setModel(new javax.swing.DefaultComboBoxModel(pathType));

        jLabel1.setText("\u503c");

        save.setText("\u4fdd\u5b58");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout
                .setHorizontalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                .createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout
                                        .createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                false)
                                        .addComponent(pathtype,
                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                0,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE)
                                        .addGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                layout
                                                .createSequentialGroup()
                                                .addComponent(
                                                        jLabel1)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(
                                                        layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(save,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                79,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(pathVal,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                109,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap(
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)));
        layout
                .setVerticalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                .createSequentialGroup()
                                .addContainerGap()
                                .addComponent(pathtype,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(
                                        layout
                                        .createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(pathVal,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel1))
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        18, Short.MAX_VALUE)
                                .addComponent(save)
                                .addContainerGap()));
        this.setResizable(false);
        pack();
    }// </editor-fold>

    // GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //ConfDialog dialog = new ConfDialog(new javax.swing.JFrame(),
                //
            }
        });
    }

    // GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton save;
    private javax.swing.JComboBox pathtype;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField pathVal;
    // End of variables declaration                   

    @Override
    public void actionPerformed(ActionEvent e) {
        target.setText(pathtype.getSelectedItem().toString());
        if (pathtype.getSelectedItem().toString().equals("id")) {
            path.setType(PathType.id);
        } else if (pathtype.getSelectedItem().toString().equals("name")) {
            path.setType(PathType.name);
        } else if (pathtype.getSelectedItem().toString().equals("className")) {
            path.setType(PathType.className);
        } else if (pathtype.getSelectedItem().toString().equals("cssSelector")) {
            path.setType(PathType.cssSelector);
        } else if (pathtype.getSelectedItem().toString().equals("linkText")) {
            path.setType(PathType.linkText);
        } else if (pathtype.getSelectedItem().toString().equals("partialLinkText")) {
            path.setType(PathType.partialLinkText);
        } else if (pathtype.getSelectedItem().toString().equals("tagName")) {
            path.setType(PathType.tagName);
        } else if (pathtype.getSelectedItem().toString().equals("xpath")) {
            path.setType(PathType.xpath);
        }
        path.setValue(pathVal.getText().replaceAll("\"", "\'"));
        this.setVisible(false);
    }

}
